package com.ruoyi.system.monitortask.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.V2.goodInfo.service.IGoodsInfoService;
import com.ruoyi.system.model.domain.GoodsInfo;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.monitortask.domain.MonitorTaskErr;
import com.ruoyi.system.monitortask.domain.MonitorTaskGoods;
import com.ruoyi.system.monitortask.mapper.MonitorTaskErrMapper;
import com.ruoyi.system.monitortask.service.IMonitorTaskErrService;
import com.ruoyi.system.monitortask.service.IMonitorTaskGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 监控任务错误表(monitor_task_err)表服务实现类
 *
 * @author zyy
 * @date 2024-06-22 11:32:53
 */
@Service("monitorTaskErrService")
public class MonitorTaskErrServiceImpl extends ServiceImpl<MonitorTaskErrMapper, MonitorTaskErr> implements IMonitorTaskErrService {

    @Autowired
    private IGoodsInfoService goodsInfoService;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private IMonitorTaskGoodsService monitorTaskGoodsService;

    @Override
    @Transactional
    public boolean handleMonitorTaskErr(MonitorTaskErr monitorTaskErr) {
        Long goodsId = monitorTaskErr.getGoodsId();
        if (monitorTaskErr.getHandleType() == 1) {
            //未处理 不用处理
        } else if (monitorTaskErr.getHandleType() == 2) {
            //正常并加入监控
            GoodsInfo goodsInfo = goodsInfoService.selectGoodsInfoById(goodsId);
            if (Objects.isNull(goodsInfo)) {
                throw new GlobalException("未找到此商品");
            }
            if (!Objects.equals(goodsInfo.getSaleState().intValue(), 1)) {
                throw new GlobalException("该商品不处于未售状态,无法加入掉绑监控");
            }
            MonitorTaskGoods monitorTaskGoods = new MonitorTaskGoods();
            monitorTaskGoods.setMonitorTaskId(monitorTaskErr.getMonitorTaskId());
            monitorTaskGoods.setGoodsIds(String.valueOf(goodsId));
            monitorTaskGoodsService.addBatch(monitorTaskGoods);
            List<Long> goodsIdList = new ArrayList<>();
            goodsIdList.add(goodsId);
            goodsInfoMapper.updateMonitoringIssues(goodsIdList, "1");
        } else if (monitorTaskErr.getHandleType() == 3) {
            //正常不加入监控 不用处理 因为在掉绑的时候 已经从监控分组中 拿掉了。
        } else if (monitorTaskErr.getHandleType() == 4) {
            //转问题号
            GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto = new GoodsInfoAndAttrValueDto();
            goodsInfoAndAttrValueDto.setId(goodsId);
            goodsInfoAndAttrValueDto.setProblemDes(monitorTaskErr.getRemark());
            goodsInfoService.problemGoods(goodsInfoAndAttrValueDto);
        }
        monitorTaskErr.setUpdateId(SecurityUtils.getUserId());
        return updateById(monitorTaskErr);
    }
}

